FORM FRM_INIT_DATA.
    REFRESH R_BWART.
    R_BWART-SIGN = 'I'.
    R_BWART-OPTION = 'EQ'.
    CASE P_OUTIN.
        WHEN '1'.
            R_BWART-LOW = '221'.
            APPEND R_BWART.
            R_BWART-LOW = '201'.
            APPEND R_BWART.
            R_BWART-LOW = '161'.
            APPEND R_BWART.
            R_BWART-LOW = '702'.
            APPEND R_BWART.
        WHEN '2'.
            R_BWART-LOW = '101'.
            APPEND R_BWART.
            R_BWART-LOW = '561'.
            APPEND R_BWART.
            R_BWART-LOW = 'W01'.
            APPEND R_BWART.
            R_BWART-LOW = '701'.
            APPEND R_BWART.
    ENDCASE.
ENDFORM.

FORM FRM_GET_DATA.
    SELECT 
    ZMMT_MAT~ZSPECNAME,
    ZMMT_MAT~ZZFG,
    ZMMT_MAT~ZMANUFACTURER,
    ZMMT00040~LGORT,
    ZMMT00040~MENGE,
    ZMMT00040~ZHWDZ,
    MSEG~ZSQBM,
    MSEG~CHARG,
    MSEG~MATNR,
    MSEG~LIFNR,
    MSEG~CPUDT_MKPF,
    ZMMT00091~EKGRP
    INTO CORRESPONDING FIELDS OF TABLE @FRM_GET_DATA
    FROM MSEG
    INNER JOIN ZMMT_MAT ON ZMMT_MAT~MATNR = MSEG~MATNR
    INNER JOIN ZMMT00040 ON ZMMT00040~MATNR = MSEG~MATNR AND ZMMT00040~CHARG = MSEG~CHARG
    INNER JOIN ZMMT00091 ON ZMMT00091~ZDJH = MSEG~ZDJH AND ZMMT00091~ZHH = MSEG~ZHH
    AND ZMMT00091~MATNR = MSEG~MATNR AND ZMMT00091~CHARG = MSEG~CHARG
    WHERE MSEG~CPUDT_MKPF IN @S_DATE
    AND MSEG~BWART IN @R_BWART
    AND ZMMT00040~LGORT IN @S_LGORT
    AND MSEG~LIFNR IN @S_LIFNR
    AND ZMMT00040~ZHWDZ IN @S_ZHWDZ
    AND ZMMT00040~MENGE <> 0
    AND MSEG~MATNR IN @S_MATNR
    AND MSEG~ZSQBM IN @S_ZSQBM
    AND ZMMT00091~EKGRP IN @S_EKGRP.

    IF GT_DATA[] IS NOT INITIAL.
        SELECT
        LFA1~NAME1,
        LFA1~LIFNR
        FROM LFA1
        INTO TABLE @DATA(LT_ITAB)
        FOR ALL ENTRIES IN @GT_DATA
        WHERE LFA1~LIFNR = @GT_DATA-LIFNR.

        SELECT
        ZMMT00093~ZHGZH,
        ZMMT00093~CHARG
        FROM ZMMT00093
        INTO TABLE @DATA(LT_ITAB2)
        FOR ALL ENTRIES IN @GT_DATA
        WHERE ZMMT00093~CHARG = @GT_DATA-CHARG.

        SELECT
        MSEG~LGORT,
        MSEG~CHARG,
        MSEG~MATNR
        FROM MSEG
        INTO TABLE @DATA(LT_ITAB3)
        WHERE LGORT IN @S_LGORT.

        SELECT
        MAKT~MAKTX,
        MAKT~MATNR
        FROM MAKT
        INTO TABLE @DATA(LT_ITAB4)
        FOR ALL ENTRIES IN @GT_DATA
        WHERE MAKT~MATNR = @GT_DATA-MATNR.

        SELECT
        MARA~MEINS,
        MARA~MATNR
        FROM LFA1
        INTO TABLE @DATA(LT_ITAB5)
        FOR ALL ENTRIES IN @GT_DATA
        WHERE MARA~MATNR = @GT_DATA-MATNR.

        SORT LT_ITAB BY LIFNR.
        SORT LT_ITAB2 BY CHARG.
        SORT LT_ITAB3 BY CHARG MATNR.
        SORT LT_ITAB4 BY MATNR.
        SORT LT_ITAB5 BY MATNR.

        LOOP AT GT_DATA.
            READ TABLE LT_ITAB INTO DATA(LS_ITAB) WITH KEY LIFNR = GT_DATA-LIFNR BINARY SEARCH.
            IF SY-SUBRC = 0.
                GT_DATA-NAME1 = LS_ITAB-NAME1.
            ENDIF.

            READ TABLE LT_ITAB2 INTO DATA(LS_ITAB2) WITH KEY CHARG = GT_DATA-CHARG BINARY SEARCH.
            IF SY-SUBRC = 0.
                GT_DATA-ZHGZH = LS_ITAB2-ZHGZH.
            ENDIF.

            READ TABLE LT_ITAB3 INTO DATA(LS_ITAB3) WITH KEY CHARG = GT_DATA-CHARG MATNR = GT_DATA-MATNR BINARY SEARCH.
            IF SY-SUBRC = 0.
                GT_DATA-LGORT1 = LS_ITAB3-LGORT+0(1) && '001'.
            ENDIF.

            READ TABLE LT_ITAB4 INTO DATA(LS_ITAB4) WITH KEY MATNR = GT_DATA-MATNR BINARY SEARCH.
            IF SY-SUBRC = 0.
                GT_DATA-MAKTX = LS_ITAB4-MAKTX.
            ENDIF.

            READ TABLE LT_ITAB5 INTO DATA(LS_ITAB5) WITH KEY MATNR = GT_DATA-MATNR BINARY SEARCH.
            IF SY-SUBRC = 0.
                GT_DATA-MEINS = LS_ITAB5-MEINS.
            ENDIF.
            MODIFY GT_DATA.
            CLEAR GT_DATA.
        ENDLOOP.
    ENDIF.
ENDFORM.

FORM FRM_PROCESS_DATA.
    IF GT_DATA[] IS NOT INITIAL.
        REFRESH LT_DATA.
        SELECT MATNR,CHARG,DMBTR,BWART,MENGE FROM MSEG
        INTO CORRESPONDING FIELDS OF TABLE @LT_DATA
        FOR ALL ENTRIES IN @GT_DATA
        WHERE MATNR = @GT_DATA-MATNR
        AND CHARG = @GT_DATA-MATNR
        AND BWART IN ('101','561').

        SORT LT_DATA BY MATNR CHARG.
        LOOP AT GT_DATA.
            READ TABLE LT_DATA WITH KEY CHARG = GT_DATA-CHARG MATNR = GT_DATA-MATNR BINARY SEARCH.
            IF SY-SUBRC = 0.
                IF LT_DATA-MENGE <> 0.
                    GT_DATA-DMBTR = LT_DATA-DMBTR / LT_DATA-MENGE * GT_DATA-MENGE.
                ENDIF.
            ENDIF.
            MODIFY GT_DATA.
            CLEAR GT_DATA.
        ENDLOOP.

        REFRESH LT_DATA.
        SELECT
        MENGE,
        DMBTR,
        CHARG,
        FROM MSEG
        INTO CORRESPONDING FIELDS OF TABLE @LT_DATA
        FOR ALL ENTRIES IN @GT_DATA
        WHERE MSEG~MATNR = @GT_DATA-MATNR
        AND MSEG~CHARG = @GT_DATA-CHARG.

        SORT LT_DATA BY CHARG MATNR.
    ENDIF.

    CASE P_OUTIN.
        WHEN '1'.
            LOOP AT GT_DATA.
                READ TABLE LT_DATA WITH KEY CHARG = GT_DATA-CHARG MATNR = GT_DATA-MATNR BINARY SEARCH.
                IF SY-SUBRC = 0.
                    GT_DATA-MRFMG = LT_DATA-MENGE.
                    GT_DATA-ZCKJE = LT_DATA-DMBTR.
                ENDIF.
                SHIFT GT_DATA-MATNR LEFT DELETING LEADING '0'.
                SHIFT GT_DATA-LIFNR LEFT DELETING LEADING '0'.

                MODIFY GT_DATA.
                CLEAR: GT_DATA.
            ENDLOOP.
            PERFORM FRM_DISPLAY_DATA.
        WHEN '2'.
        LOOP AT GT_DATA.
            READ TABLE LT_DATA WITH KEY CHARG = GT_DATA-CHARG MATNR = GT_DATA-MATNR BINARY SEARCH.
            IF SY-SUBRC = 0.
                GT_DATA-ZRKSL = LT_DATA-MENGE.
                GT_DATA-ZRKJE = LT_DATA-DMBTR.
            ENDIF.
            SHIFT GT_DATA-MATNR LEFT DELETING LEADING '0'.
            SHIFT GT_DATA-LIFNR LEFT DELETING LEADING '0'.

            MODIFY GT_DATA.
            CLEAR: GT_DATA.
        ENDLOOP.
        PERFORM FRM_DISPLAY_DATA2.
    ENDCASE.
ENDFORM.